<?php
declare (strict_types=1);

namespace app\common\model;

use app\common\facade\Jpush;
use think\facade\Event;
use think\model\relation\BelongsTo;
/**
 * 会员等级日志
 * Class UserLevelLog
 * @package app\common\model
 */
class UserLevelLog extends Base
{
    protected $name = 'user_level_log';

    //类型：1会员等级 2商城代理 3分红级别 11合约专区团队等级 12合约专区代理 13合约专区角色  21云牧场养殖等级 22云牧场代理等级
    public static $_type = [
        1 => '会员等级',
        2 => '商城代理',
        3 => '分红级别',
        11 => '合约专区团队等级',
        12 => '合约专区代理',
        13 => '合约专区角色',
        21 => '云牧场养殖等级',
        22 => '云牧场代理',
        31 => '云耕科技养殖等级',
        32 => '云耕科技代理'
    ];

    public function getTypeTextAttr($value, $data): string
    {
        return isset(self::$_type[$data['type']]) ? self::$_type[$data['type']] : '未知';
    }

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class, 'uid', 'id');
    }

    public function getAreaAttr($value, $data): string
    {
        if($data['area_id']){
            $area = Area::where('code', $data['area_id'])->find();
            if($area->level == 2){
                return $area->city;
            }else{
                return $area->city.$area->district;
            }
        }
        return '';
    }

    /**
     * 会员等级更改日志
     * @param $user
     * @param $type
     * @param $before_id
     * @param $after_id
     * @param null $admin
     * @param array $param
     * @return bool
     */
    public static function levelLog($user, $type, $before_id, $after_id, $admin=null, $param=[]): bool
    {
        if($after_id == $before_id) return false;
        switch ($type){
            case 1://会员等级
                $before = UserLevel::where('id', $before_id)->value('title')??'';
                $after = UserLevel::where('id', $after_id)->value('title')??'';
                $intro = "会员等级由【{$before}】变更为【{$after}】";

                if($after_id == 2){
                    $user->vip_time = time();

                }elseif ($after_id == 3 || $after_id == 4){
                    $user->shoper_time = time();
                    $user->exp_shoper_time = strtotime("+1 year");
                    if($admin){
                        $data = ['order_type'=>2, 'uid'=>$user->id, 'has_log'=>1];
                        $data = json_decode(json_encode($data));
                        Event::trigger('UserLevel', $data);//后台修改会员级别为店主，也要触发高级店主和盟主的升级
                    }
                }elseif($after_id == 5){
                    $user->leader_time = time();
                    $user->exp_leader_time = strtotime("+1 year");
                }
                $user->exp_time = strtotime(User::getExpTime($user));
                if($user->agent_level == 0){
                    $user->syn_level = $after_id;
                }
                $user->level_id = $after_id;
                $user->save();

                if($after_id>$before_id){
                    if(!($before_id == 3 && $after_id == 4)){
                        $user->before_level_id = $before_id;
                        Event::trigger('LeaderScore', $user);//增加推荐分,由店主升级为高级店主不加分
                    }
                }elseif($after_id<$before_id){
                    $user->is_demotion = 1;
                    if(!($before_id == 4 && $after_id == 3)){
                        $user->before_level_id = $before_id;
                        Event::trigger('LeaderScore', $user);//减少推荐分,由高级店主降为店主不减分
                    }
                }
                break;
            case 3://分红级别
                $before = UserLevelBonus::where('id', $before_id)->value('title')??'';
                $after = UserLevelBonus::where('id', $after_id)->value('title')??'';
                $intro = "会员分红级别由【{$before}】变更为【{$after}】";
                break;
            case 11://合约专区团队等级
                $before = ContractLevel::where('id', $before_id)->value('title')??'';
                $after = ContractLevel::where('id', $after_id)->value('title')??'';
                $intro = "合约专区会员团队等级由【{$before}】变更为【{$after}】";
                break;
            case 13://合约专区角色
                $contract_role = contractRole();
                $before = isset($contract_role[$before_id]) ? $contract_role[$before_id] :'未知';
                $after = isset($contract_role[$after_id]) ? $contract_role[$after_id] :'未知';
                $intro = "合约专区会员角色由【{$before}】变更为【{$after}】";
                break;
            case 21://云牧场养殖等级
                $before = UserBreed::where('id', $before_id)->value('title')??'';
                $after = UserBreed::where('id', $after_id)->value('title')??'';
                $intro = "云牧场会员养殖等级由【{$before}】变更为【{$after}】";
                break;
            case 31://云耕科技养殖等级
                $before = UserTillage::where('id', $before_id)->value('title')??'';
                $after = UserTillage::where('id', $after_id)->value('title')??'';
                $intro = "云耕科技会员养殖等级由【{$before}】变更为【{$after}】";
                break;
            default:
                return false;

        }
        UserLevelLog::create([
            'uid' => $user->id,
            'type' => $type,
            'before_id' => $before_id,
            'after_id' => $after_id,
            'before' => $before,
            'after' => $after,
            'intro' => $intro,
            'admin_id' => $admin ? $admin['id'] : '',
            'admin_name' => $admin ? $admin['admin_name'] : '',
            'relation_uid' => !empty($param['relation_uid']) ? $param['relation_uid'] : 0
        ]);
        return true;
    }
}
